package com.example.clublesson01.login

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.kf.lib_base.base.BaseActivity
import com.example.clublesson01.R
import com.example.clublesson01.databinding.ActivityLoginBinding
@Route(path = "/app/login/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginRepo>() {
    override fun iniView() {
        //返回上一页
        binding.ivBack.setOnClickListener {
            finish()
        }
        //富文本
        val span= SpannableStringBuilder(binding.fwb.text.toString())
        span.setSpan(ForegroundColorSpan(Color.RED),5,11,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        val chickenspan=object: ClickableSpan(){
            @SuppressLint("SuspiciousIndentation")
            override fun onClick(widget: android.view.View) {
                ARouter.getInstance().build("/app/web/WebActivity")
                    .withString("url","https://www.bilibili.com")
                    .navigation()
            }
        }
        span.setSpan(chickenspan,12,18,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.fwb.movementMethod= LinkMovementMethod.getInstance()
        binding.fwb.text=span
        binding.btnLogin.setOnClickListener {
            viewModel.Login(binding.username.text.toString(),
                binding.password.text.toString())
        }

        binding.tvWang.setOnClickListener {
            ARouter.getInstance().build("/app/login/WangLoginActivity").navigation()
        }
    }
    override fun iniData() {
        viewModel.success.observe(this){
            if (it.code==200){
                SPUtils.getInstance().put("token",it.data.access_token)
                ToastUtils.showShort("登录成功")
                //ARouter跳转
                ARouter.getInstance().build("/app/MainActivity").navigation()
            }else{
                ToastUtils.showShort("登录失败")
            }
        }
        viewModel.fall.observe(this){
            ToastUtils.showShort(it.toString())
        }
    }
    override fun getViewModel(): Class<LoginRepo> {
        return LoginRepo::class.java
    }

    override fun Layoutid(): Int {
        return R.layout.activity_login
    }

}